package top.cardone.api.v1.usercenter.department

import org.apache.commons.lang3.StringUtils
import top.cardone.usercenter.service.DepartmentService
import top.cardone.context.ApplicationContextHolder
import top.cardone.core.CodeException

class u0001 implements java.io.Serializable {
    def input(input) {
        def newInput = [:]

		newInput['departmentId'] = input['departmentId']
		newInput['name'] = input['name']
		newInput['departmentCode'] = input['departmentCode']
		newInput['parentCode'] = input['parentCode']
		newInput['flagCode'] = StringUtils.defaultIfBlank(input['flagCode'], "input")

        newInput
    }

    def validation(input) {
//      if (StringUtils.isBlank(input?.departmentId)) {
//          throw new CodeException("departmentId required", "部门标识必填")
//      }
//
//      if (StringUtils.isBlank(input?.name)) {
//          throw new CodeException("name required", "部门名称必填")
//      }
//
//      if (StringUtils.length(input?.name) < 4) {
//          throw new CodeException("name minlength", "部门名称需{0}个字符以上", 4)
//      }
//
//      if (StringUtils.length(input?.name) > 255) {
//          throw new CodeException("name maxlength", "部门名称需小于{0}个字符", 255)
//      }

		def readOne = ["departmentCode": input.departmentCode, "object_id": "departmentId"]

		String dbDepartmentId = ApplicationContextHolder.getBean(DepartmentService.class).readOne(String.class, readOne)

		if ((dbDepartmentId != null) && !StringUtils.equals(dbDepartmentId, input.departmentId)) {
			throw new CodeException("departmentId already exists", "部门编号已经存在")
		}
    }

    def func(input) {
		['updateCount': ApplicationContextHolder.getBean(DepartmentService.class).updateCache(input)]
    }

    def output(output) {
        output
    }
}